package cn.zyq.ykb.service.sys.impl;


import cn.zyq.ykb.beans.sys.SysAdmin;
import cn.zyq.ykb.beans.sys.SysNotice;
import cn.zyq.ykb.mapper.sys.SysNoticeMapper;
import cn.zyq.ykb.service.sys.SysNoticeService;
import cn.zyq.ykb.system.common.BaseService;
import cn.zyq.ykb.system.common.PageParam;
import cn.zyq.ykb.system.ry.util.SecurityUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 通知公告表
 * 2023-05-12 20:14:09
 */

@Slf4j
@Service
public class SysNoticeServiceImpl extends BaseService implements SysNoticeService {
    @Autowired
    private SysNoticeMapper sysNoticeMapper;


    @Override
    public PageInfo<SysNotice> list(PageParam pageParam, SysAdmin admin) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        List<SysNotice> dbList = this.sysNoticeMapper.list(pageParam.getKv());
        return new PageInfo<>(dbList);
    }

    @Override
    public void add(SysNotice param, SysAdmin admin) {
        this.setCreateBaseData(param, admin);
        this.sysNoticeMapper.insertSelective(param);
    }

    @Override
    public void update(SysNotice param, SysAdmin admin) {
        Integer id = param.getSysNoticeID();
        this.setUpdateBaseData(param, admin);
        this.sysNoticeMapper.updateByPrimaryKeySelective(param);
    }

    @Override
    public void del(String serviceID, SysAdmin admin) {
        SysNotice dbInfo = this.detail(serviceID);
        Integer id = dbInfo.getSysNoticeID();
        this.deleteLogic(id, admin, this.sysNoticeMapper);
    }

    @Override
    public SysNotice detail(String serviceID, SysAdmin admin) {
        return this.detail(serviceID);
    }

    @Override
    public SysNotice detail(String serviceID) {
        SysNotice dbInfo = this.getByServiceID(serviceID, SysNotice.class, this.sysNoticeMapper);
        this.checkLogicExist(dbInfo);
        return dbInfo;
    }

    /**
     * 查询公告信息
     *
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public SysNotice selectNoticeById(Integer noticeId) {
        return sysNoticeMapper.selectByPrimaryKey(noticeId);
    }

    /**
     * 查询公告列表
     *
     * @param param 公告信息
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectNoticeList(PageParam param) {
        return sysNoticeMapper.list(param.getKv());
    }

    /**
     * 新增公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int insertNotice(SysNotice notice) {
        this.setCreateBaseData(notice, SecurityUtils.getLoginUser().getSysAdmin());
        return this.sysNoticeMapper.insertSelective(notice);
    }

    /**
     * 修改公告
     *
     * @param param 公告信息
     * @return 结果
     */
    @Override
    public int updateNotice(SysNotice param) {
        this.setUpdateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.sysNoticeMapper.updateByPrimaryKeySelective(param);
    }

    /**
     * 删除公告对象
     *
     * @param noticeId 公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeById(Integer noticeId) {
        return sysNoticeMapper.deleteByPrimaryKey(noticeId);
    }

    /**
     * 批量删除公告信息
     *
     * @param noticeIds 需要删除的公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeByIds(Integer[] noticeIds) {
        int i = 0;
        for (Integer noticeId : noticeIds) {
            sysNoticeMapper.deleteByPrimaryKey(noticeId);
            i++;
        }
        return i;
    }
}
